package com.flea.hclv.controller;

import com.alibaba.fastjson.JSON;
import com.flea.hclv.model.entity.SpotTicket;
import com.flea.hclv.service.HSpotTicketService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.base.util.StringUtil;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.ControllerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/flea/hclv/spotTicket/")
public class HSpotTicketController {

    @Autowired
    private HSpotTicketService hSpotTicketService;

    @RequestMapping("saveOrUpdateSpotTicket")
    @ResponseBody
    public ServerResponse<String> saveOrUpdateHotel(HttpServletRequest request, HttpServletResponse response) {
        String json = request.getParameter("model");
        String spotId = request.getParameter("spotId");
        if (spotId == null) {
            spotId = JSON.parseObject(json).get("spotId").toString();
        }
        SpotTicket spotTicket = JSON.parseObject(json, SpotTicket.class);
        String userId = FleaOrgUtils.getCurUserUuid(request);
        ServerResponse<String> response1 = hSpotTicketService.saveOrUpdate(spotTicket, userId, spotId);
        return response1;
    }

    /**
     * 查询所有的门票
     *
     * @param request  request
     * @param response response
     * @param paging   分页对象
     */
    @RequestMapping("selectAll")
    public void selectAll(HttpServletRequest request, HttpServletResponse response, FleaPage<SpotTicket> paging) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        FleaPage<SpotTicket> spotVos = hSpotTicketService.selectAll(userId, paging.getCurPage(), paging.getPageSize());
        ControllerResult cResult = new ControllerResult(true, "操作成功", spotVos);
        cResult.WriteToPage(response);
    }

    /**
     * 查询一个组团社供应商门票
     *
     * @param request request
     * @param paging  分页对象
     * @return list
     */
    @RequestMapping("selectByDeptId")
    @ResponseBody
    public ServerResponse<FleaPage<SpotTicket>> selectByDeptId(HttpServletRequest request, FleaPage<SpotTicket> paging,
                                                               @RequestParam(value = "spotName", defaultValue = "") String spotName,
                                                               @RequestParam(value = "spotId", defaultValue = "") String spotId) {
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        if (currentUserDept == null) {
            return ServerResponse.createByErrorMessage("没有找到该用户的部门信息");
        }
        return hSpotTicketService.selectByDeptId(currentUserDept.getDeptUuid(), paging.getCurPage(), paging.getPageSize(), spotId,spotName);
    }

    @RequestMapping("deleteByIds")
    public void selectAll(HttpServletRequest request, HttpServletResponse response, @RequestParam("ids") String spotTicketIds) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        List<String> ids = JSON.parseArray(spotTicketIds, String.class);
        boolean result = hSpotTicketService.deleteByIds(ids, userId);
        ControllerResult cResult = new ControllerResult(true, "操作成功", result);
        cResult.WriteToPage(response);
    }

    /**
     * 查询某一个景区的门票（某个景区）
     *
     * @param
     * @param request  request
     * @param response response
     * @param paging   分页对象
     */
    @RequestMapping("selectBySpotId")
    @ResponseBody
    public ServerResponse<FleaPage<SpotTicket>> selectBySpotId(HttpServletRequest request,
                                                               HttpServletResponse response, FleaPage<SpotTicket> paging, String spotId) throws IOException {
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        if (currentUserDept == null) {
            return ServerResponse.createByErrorMessage("没有找到部门信息");
        }

        if (StringUtil.isEmpty(spotId)) {
            return ServerResponse.createByErrorMessage("景点ID为空");
        } else {
            ServerResponse<FleaPage<SpotTicket>> spotTickets = hSpotTicketService.selectBySpotId(spotId, paging.getCurPage(), paging.getPageSize(), currentUserDept.getDeptUuid());
            return spotTickets;
        }
    }

    //所有供应商门票
    @RequestMapping("selectBygDeptId")
    @ResponseBody
    public ServerResponse<FleaPage<SpotTicket>> selectBygDeptId(HttpServletRequest request, FleaPage<SpotTicket> paging,
                                                                @RequestParam(value = "spotName", defaultValue = "") String spotName) {

        ServerResponse<FleaPage<SpotTicket>> response = hSpotTicketService.selectBygDeptId(paging.getCurPage(), paging.getPageSize(),spotName);
        return response;
    }


//    private String getData(HttpServletRequest req) throws IOException {
//        // 获取post参数
//        StringBuffer sb = new StringBuffer();
//        InputStream is = req.getInputStream();
//        InputStreamReader isr = new InputStreamReader(is);
//        BufferedReader br = new BufferedReader(isr);
//        String s = "";
//        while ((s = br.readLine()) != null) {
//            sb.append(s);
//        }
//        return sb.toString();
//    }
}
